package com.blqy.fr.user.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class MailService {

    @Autowired
    private JavaMailSender mailSender;

    public void sendVerificationCode(String to, String code) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("lgc0221@163.com");
        message.setTo(to);
        message.setSubject("Fruity-Robo邮箱验证码");
        message.setText("您的验证码是：" + code + "，5分钟内有效。");
        try {
            mailSender.send(message);
            log.info("验证码邮件发送成功，收件人: {}", to);
        } catch (Exception e) {
            log.error("发送验证码邮件失败，收件人: {}, 错误: {}", to, e.getMessage(), e);
            throw new RuntimeException("邮件发送失败，请稍后再试");
        }
    }
}